/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.manager.impl;

import java.util.Collection;
import java.util.List;

import com.xy.biz.ts.dao.ExaminationQuestionDao;
import com.xy.biz.ts.dao.ExaminationQuestionOptionDao;
import com.xy.biz.ts.dao.ExaminationRecordQuestionDao;
import com.xy.biz.ts.domain.bo.ExaminationQuestionBO;
import com.xy.biz.ts.domain.po.ExaminationQuestionPO;
import com.xy.biz.ts.domain.po.ExaminationRecordPO;
import com.xy.biz.ts.domain.po.ExaminationRecordQuestionPO;
import com.xy.biz.ts.domain.query.ExaminationRecordQuestionQuery;
import com.xy.biz.ts.manager.ExaminationQuestionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.biz.ts.domain.bo.ExaminationRecordBO;
import com.xy.biz.ts.domain.query.ExaminationRecordQuery;
import com.xy.biz.ts.domain.converter.ExaminationRecordConverter;
import com.xy.biz.ts.manager.ExaminationRecordManager;
import com.xy.biz.ts.dao.ExaminationRecordDao;

/**
 * @author wcs
 * @date 2021-3-10
 */
@Service
public class ExaminationRecordManagerImpl implements ExaminationRecordManager {

    @Autowired
    private ExaminationRecordDao examinationRecordDao;

    @Autowired
    private ExaminationQuestionDao examinationQuestionDao;

    @Autowired
    private ExaminationQuestionOptionDao examinationQuestionOptionDao;

    @Autowired
    private ExaminationRecordQuestionDao examinationRecordQuestionDao;

    @Autowired
    private ExaminationQuestionManager examinationQuestionManager;

    @Override
    public ExaminationRecordBO saveExaminationRecord(ExaminationRecordBO examinationRecordBO) {
        ExaminationRecordPO examinationRecordPO = ExaminationRecordConverter.toPO(examinationRecordBO);
        int row = examinationRecordDao.saveExaminationRecord(examinationRecordPO);
        if (row > 0) {
            return ExaminationRecordConverter.fromPO(examinationRecordPO);
        }
        return null;
    }

    @Override
    public int saveAllExaminationRecord(Collection<ExaminationRecordBO> collection) {
        return examinationRecordDao.saveAllExaminationRecord(ExaminationRecordConverter.toPO(collection));
    }

    @Override
    public int updateExaminationRecord(ExaminationRecordBO examinationRecordBO) {
        return examinationRecordDao.updateExaminationRecord(ExaminationRecordConverter.toPO(examinationRecordBO));
    }

    @Override
    public int deleteExaminationRecord(Long examinationRecordId) {
        return examinationRecordDao.deleteExaminationRecord(examinationRecordId);
    }

    @Override
    public ExaminationRecordBO getExaminationRecord(Long examinationRecordId) {
        ExaminationRecordPO examinationRecord = examinationRecordDao.getExaminationRecord(examinationRecordId);
        ExaminationRecordQuestionQuery query = ExaminationRecordQuestionQuery.max().examinationRecordId(examinationRecordId).build();
        List<ExaminationRecordQuestionPO> recordQuestionList = examinationRecordQuestionDao.listExaminationRecordQuestion(query);
        recordQuestionList.stream().forEach(e -> e.setExaminationQuestion(buildExaminationQuestion(e.getExaminationQuestionId())));
        examinationRecord.setRecordQuestionList(recordQuestionList);
        return ExaminationRecordConverter.fromPO(examinationRecord);
    }

    private ExaminationQuestionBO buildExaminationQuestion(Long examinationQuestionId) {
        ExaminationQuestionBO examinationQuestion = examinationQuestionManager.getExaminationQuestion(examinationQuestionId);
        return examinationQuestion;
    }

    @Override
    public List<ExaminationRecordBO> listExaminationRecordById(Collection<Long> collection) {
        return ExaminationRecordConverter.fromPO(examinationRecordDao.listExaminationRecordById(collection));
    }

    @Override
    public List<ExaminationRecordBO> listExaminationRecord(ExaminationRecordQuery query) {
        return ExaminationRecordConverter.fromPO(examinationRecordDao.listExaminationRecord(query));
    }

    @Override
    public int countExaminationRecord(ExaminationRecordQuery query) {
        return examinationRecordDao.countExaminationRecord(query);
    }

}
